package cn.fufeiii.bt.nomiddleware.print.controller;

import cn.hutool.core.io.IoUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @author Dark.Fu
 */
@Slf4j
@RestController
public class CkoPrintController {

    @RequestMapping("/cko/event/print")
    public void print(HttpServletRequest request) throws IOException {
        String body = IoUtil.read(request.getInputStream(), StandardCharsets.UTF_8);
        Map<String, Object> map = new ObjectMapper().readValue(body, new TypeReference<Map<String, Object>>() {
        });
        String authorization = request.getHeader("authorization");
        String signature = request.getHeader("cko-signature");
        log.info("----------------------------------------------------");
        log.info("eventType = {}", map.get("type"));
        log.info("requestMethod = {}", request.getMethod());
        log.info("header -> authorization={} signature={}", authorization, signature);
        log.info("origin body = {}", body);
        log.info("body = {}", map);
        log.info("----------------------------------------------------\n");
    }

}
